﻿using DataSync.Entity;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace DataSync.Proc.CustParse
{
    class RegexCustParse : ICustParse
    {
        private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public void Init(XElement xmlnode)
        {
            
        }

        public string Parse(string curValue, object[] rowValues, string custParams, CustParseExtData extParams)
        {
            string NewValue = curValue;
            try
            {
                var CovParam = JsonConvert.DeserializeObject<RegexCustParseParams>(custParams);
                Regex r = new Regex(CovParam.RegexPattern);
                NewValue = r.Match(curValue).Value;
            }
            catch (Exception ex)
            {
                log.Warn(string.Format("格式转换出错:{0}", curValue), ex);
            }
            return NewValue;
        }
    }

    class RegexCustParseParams
    {
        public string RegexPattern { get; set; }
    }
}
